<?php
/**
 * 
 * 抽象Action,所有控制器的基类
 * @author LeoNing 2010-12-18
 * @version 1.0
 * @since 1.0
 */
abstract class Action {
	protected $action_name;
	protected $meta;
	protected $app;
	protected $xajax;
	public $uri = '';
	public $referer = '';
	public $appuri = '';
	public $url = '';
	public $siteurl = '/';
	public $__act_arg = 'act';
	public $entry;
	var $type = 'normal';
	public function __construct($app, $entry = '') {
		global $__cmd;
		$this->app = $app;
		$this->entry = $entry;
		$this->action_name = preg_replace ( '/Action$/', '', get_class ( $this ) );
		$this->siteurl = site_url ();
		$this->appuri = $this->siteurl . $entry . '/';
		$this->uri = $this->appuri . '?' . $this->__act_arg . '=' . $this->action_name;
		$this->url = $this->appuri . '?' . $_SERVER ['QUERY_STRING'];
		$this->xajax = isset ( $_SERVER ["HTTP_X_AJAX_TYPE"] ) ? $_SERVER ["HTTP_X_AJAX_TYPE"] : '';
		$this->referer = isset ( $_SERVER ['HTTP_REFERER'] ) ? $_SERVER ['HTTP_REFERER'] : $this->siteurl;
		$__cmd = $this;
	}
	
	/**
	 * 返回应用信息,过时(不可用)
	 * 
	 * @deprecated
	 * @return AppMeta 返回应用信息
	 */
	public function getMeta() {
		return $this->meta;
	}
	/**
	 * 在执行命令前执行
	 * @param IRequest $request
	 * @param Response $response
	 * @param Ioc	   $ioc
	 * @return boolean 
	 */
	public function beforeExec($request, $response, $ioc) {
		return true;
	}
	/**
	 * 执行命令
	 * @param IRequest $request
	 * @param Response $response
	 * @param Ioc	   $ioc
	 * @return boolean 返回响应
	 */
	public abstract function exec($request, $response, $ioc);
	/**
	 * 当前的请求是否被缓存,如果被缓存则返回缓存KEY,反之返回false
	 * @param IRequest $request	 
	 * @param Ioc	   $ioc
	 * @return mixed 缓存返回缓存KEY,反之返回false
	 */
	public function cacheKey($request, $ioc) {
		return false;
	}
	/**
	 * 
	 * 在页面绘制之前被调用,可以在这儿注册一个页面上的资源
	 * @param IRequest $request
	 * @param Response $response
	 * @param Ioc $ioc
	 */
	public function beforeRender($request, $response, $ioc) {
	}
	
	//输出自定义头部信息
	public function do_page_header() {
	}
	//输出自定义脚本信息
	public function do_page_footer() {
	}
	/**
	 * 
	 * 根据一个$method指定的参数进行分发操作
	 * @param string $method 可以指定操作的参数
	 * @param IRequest $req
	 * @param Response $res
	 * @param Ioc $ioc
	 * @param strint $pre 前缀
	 */
	protected function dispatch($method, $req, $res, $ioc, $pre = "m_") {
		$checked = false;
		if (is_array ( $method )) {
			foreach ( $method as $md ) {
				if ($req->has ( $md )) {
					$method = $md;
					if (is_array ( $pre )) {
						$pre = isset ( $pre [$md] ) ? $pre [$md] : "{$md}_";
					}
					$checked = true;
					break;
				}
			}
		}
		$method = ! $checked ? (is_array ( $method ) ? $method [0] : $method) : $method;
		$pre = ! $checked ? (is_array ( $pre ) ? array_shift ( $pre ) : $pre) : $pre;
		$method = $pre . $req->get ( $method, 'index' );
		if (is_callable ( array ($this, $method ) )) {
			return call_user_func_array ( array ($this, $method ), array ($req, $res, $ioc ) );
		} else {
			if ($this->xajax) { //ajax调用了一个不存在的方法
				if ($this->xajax == 'json') { //以json方式调用
					$res->view = 'jsonView';
					$res->assign ( array ('success' => false, 'msg' => '未知操作!' ) );
				} else {
					$res->begin ();
					$res->out ( '<div class="ajax-err-msg">未知操作!</div>' );
					return false;
				}
			} else {
				smsg ( '操作未定义!', 'att ra' );
				$res->redirect ( $this->referer ); //返回来源页面
			}
		}
	}
	/**
	 * 加载校验规则
	 * @param string $rule 校验规则名称
	 * @return arary 校验规则集
	 */
	protected function rule($rule) {
		$frule = APPS . $this->app . DS . 'validates' . DS . $rule . '.json';
		if (is_readable ( $frule )) {
			$rulec = file_get_contents ( $frule );
			$rulec = preg_replace ( '/[\\r\\n\\t]/', '', $rulec );
			$rules = @json_decode ( $rulec, true );
			if (! empty ( $rules ) && is_readable ( APPS . $this->app . DS . 'validates' . DS . $rule . '.inc' )) {
				include_once APPS . $this->app . DS . 'validates' . DS . $rule . '.inc';
				return $rules;
			}
			return empty ( $rules ) ? array () : $rules;
		}
		return array ();
	}
	/**
	 * 注册一个CSS
	 * 
	 * @param string $resource css url
	 * 
	 */
	protected function stylesheet($resource) {
		if (is_array ( $resource )) {
			foreach ( $resource as $res ) {
				pagecss ( $res, 10 );
			}
		} else {
			pagecss ( $resource, 10 );
		}
	}
	/**
	 * 
	 * 注册一个JS
	 * @param string  $resource js url
	 * @param boolean $header 是否在头部加载
	 */
	protected function script($resource, $header = true) {
		if (is_array ( $resource )) {
			foreach ( $resource as $res ) {
				pagejs ( $res, 10, $header );
			}
		} else {
			pagejs ( $resource, 10, $header );
		}
	}
	/**
	 * 注册当前入口下的js
	 * 
	 * @param string $js
	 * @param boolean $header
	 */
	protected function myscript($js, $header = true) {
		if (is_array ( $js )) {
			foreach ( $js as $_js ) {
				pagejs ( res ( 'js/' . $_js, $this->entry ), 10, $header );
			}
		} else {
			pagejs ( res ( 'js/' . $js, $this->entry ), 10, $header );
		}
	}
	/**
	 * 注册当前入口下的css
	 * 
	 * @param string $css	
	 */
	protected function mystyle($css) {
		if (is_array ( $css )) {
			foreach ( $css as $cs ) {
				pagecss ( res ( 'style/' . $cs, $this->entry ), 10 );
			}
		} else {
			pagecss ( res ( 'style/' . $css, $this->entry ), 10 );
		}
	}
	/**
	 * 
	 * 引用全局的JS文件
	 * @param string $js
	 * @param boolean $header
	 */
	protected function resscript($js, $header = true) {
		if (is_array ( $js )) {
			foreach ( $js as $j ) {
				pagejs ( res ( $j ), 10, $header );
			}
		} else {
			pagejs ( res ( $js ), 10, $header );
		}
	}
	/**
	 * 
	 * 引用全局的CSS文件
	 * @param string $css
	 */
	protected function resstyle($css) {
		if (is_array ( $css )) {
			foreach ( $css as $cs ) {
				pagecss ( res ( $cs ), 10 );
			}
		} else {
			pagecss ( res ( $css ), 10 );
		}
	}
	/**
	 * 
	 * 生成一段有用的jquery代码
	 * @param PQ $pq
	 */
	protected function jquery($pq) {
		jquery ( $pq );
	}
	/**
	 * 
	 * 填表
	 * @param array $formvars key=>value
	 */
	protected function fillform($formvars) {
		setformvars ( $formvars );
	}
	/**
	 * 
	 * 生成URL
	 * @param string $action
	 * @param string $app
	 * @param string $args
	 */
	protected function mkurl($action = '', $app = null, $args = '') {
		$url = $app == null ? $this->appuri : $url = $this->siteurl . strtolower ( $app ) . '/';
		if (! empty ( $action )) {
			$args = $this->__act_arg . '=' . $action . '&' . $args;
		}
		return empty ( $args ) ? $url : $url . '?' . $args;
	}
	/**
	 * 生成跳转URL
	 * 
	 * @param IRequest $request
	 * @param Response $response
	 * @param Ioc $ioc
	 */
	protected function getJumpUrl($request, $response, $ioc) {
		if ($this->xajax) {
			return $this->referer;
		} else {
			return $this->url;
		}
	}
}